package com.ymatou.iapi.commission.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.commission.parameter.ChargeCommissionRateRequestBean;
import com.ymatou.iapi.commission.parameter.ProductInfoModel;
import com.ymatou.iapi.commission.service.ChargeCommissionRateServiceCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.mongo.CommissionWrapper;

public class Ts_ChargeCommissionRate {
	
	private static ChargeCommissionRateRequestBean chargeCommissionRateRequestBean;
	private static ChargeCommissionRateServiceCall chargeCommissionRateRequestCall;
	private static CommissionWrapper rateTypeWrapper = new CommissionWrapper("RateType");
	private static CommissionWrapper sellerRateTypeWrapper = new CommissionWrapper("SellerRateType");
	private static CommissionWrapper dimensionComboWrapper = new CommissionWrapper("DimensionCombo");
	private static CommissionWrapper rateStrategyWrapper = new CommissionWrapper("RateStrategy");
	private static long sellerId = 443322;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("收佣接口");
		//添加费率测试用户
		rateTypeWrapper.saveAndUpdateRateType();
		//添加维度组合
		dimensionComboWrapper.saveAndUpdateDimensionCombo();
		//添加买手与费率关系
		sellerRateTypeWrapper.saveAndUpdateSellerRateType("2018-03-01 00:00:00", "2018-05-01 00:00:00");
		//添加费率策略
		rateStrategyWrapper.saveAndUpdateRateStrategy("2018-03-01 00:00:00", "2018-05-01 00:00:00");
	}

	@Before
	public void caseUp() {
		chargeCommissionRateRequestBean = new ChargeCommissionRateRequestBean();
		chargeCommissionRateRequestCall = new ChargeCommissionRateServiceCall();
		
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(chargeCommissionRateRequestCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChargeCommissionRate_001() {
		Logger.start(true,"二级类目+国家收佣");
		try {
			Logger.comment("前置数据准备");
			long orderId = 11111;
			String orderPaidTime = "2018-03-22 14:13:17";
			long userId = 1234;
			long sellerId = 443322;
			int countryId = 1;
			boolean oldCustomer = false;
			boolean sellerAgent = false;
			
			ArrayList<ProductInfoModel> productInfoList = new ArrayList<ProductInfoModel>();
			ProductInfoModel info1 = new ProductInfoModel();
			String catalogId = "catalogId1";
			String secCategoryId = "1233";
			BigDecimal productPrice = new BigDecimal(53);
			
			info1.setCatalogId(catalogId);
			info1.setSecCategoryId(secCategoryId);
			info1.setProductPrice(productPrice);
			
			productInfoList.add(info1);
			Logger.comment("前置数据准备完毕");
			
			chargeCommissionRateRequestBean.setOrderId(orderId);
			chargeCommissionRateRequestBean.setOrderPaidTime(orderPaidTime);
			chargeCommissionRateRequestBean.setUserId(userId);
			chargeCommissionRateRequestBean.setSellerId(sellerId);
			chargeCommissionRateRequestBean.setCountryId(countryId);
			chargeCommissionRateRequestBean.setOldCustomer(oldCustomer);
			chargeCommissionRateRequestBean.setSellerAgent(sellerAgent);
			chargeCommissionRateRequestBean.setProductInfoList(productInfoList);
			
			chargeCommissionRateRequestCall.setData(chargeCommissionRateRequestBean);
			chargeCommissionRateRequestCall.callService();
			this.verifyNormal(chargeCommissionRateRequestCall, productInfoList, orderPaidTime, productPrice);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	/**
	 * 一般性验证
	 * 
	 * @return
	 * @throws JSONException 
	 */
	public void verifyNormal(ChargeCommissionRateServiceCall chargeCommissionRateRequestCall, 
			ArrayList<ProductInfoModel> productInfoList, String orderPaidTime, 
			BigDecimal price) throws JSONException {
		
		Logger.verifyEquals("success", 
				chargeCommissionRateRequestCall.getJSONReturnData().getString("resultMessage"), 
				"验证resultMessage");
		Logger.verifyEquals("0", 
				chargeCommissionRateRequestCall.getJSONReturnData().getString("resultCode"), 
				"验证resultCode");
		Logger.verifyEquals(true, 
				chargeCommissionRateRequestCall.getJSONReturnData().getBoolean("success"), 
				"验证success");
		JSONArray productCommissionList = chargeCommissionRateRequestCall.getProductCommissionList();
		
		//退佣接口走配置，统一输出，catalogId为请求的catalogId
		for (int i = 0; i < productCommissionList.length(); i++) {
			
			String rateTypeId = sellerRateTypeWrapper.getSellerRateType(sellerId, orderPaidTime);
			Map<String, Object> map = rateStrategyWrapper.getRateStrategy(rateTypeId, orderPaidTime, price);
			
			Logger.verifyEquals(map.get("Rate"), 
					productCommissionList.getJSONObject(i).getInt("refundCommissionRate"), 
					"验证refundCommissionRate");
			Logger.verifyEquals(productInfoList.get(i).getCatalogId(), 
					productCommissionList.getJSONObject(i).getString("catalogId"), 
					"验证catalogId");
			Logger.verifyEquals(map.get("strategyId"), 
					productCommissionList.getJSONObject(i).getString("strategyId"),
					"验证strategyId");
			Logger.verifyEquals(map.get("maxCommissionAmount"), 
					productCommissionList.getJSONObject(i).getString("maxCommissionAmount"),
					"验证maxCommissionAmount");
			Logger.verifyEquals(map.get("minCommissionAmount"), 
					productCommissionList.getJSONObject(i).getString("minCommissionAmount"),
					"验证minCommissionAmount");
		}
	}
}
